﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.OleDb;

namespace Web_HengliSeed.BLL
{
    public class SysLog
    {
        /// <summary>
        /// 写系统日志
        /// </summary>
        /// <param name="strNote"></param>
        /// <param name="logClass"></param>
        /// <returns></returns>
        public static bool AddSysLog(string strNote,int logClass)
        {
            try
            {
            OleDbParameter[] parm = { new OleDbParameter("@T_LogClass",logClass),
                                                       new OleDbParameter("@T_LogNote",strNote),
                                                       new OleDbParameter("@T_LogIP",Common.GetMes.GetIp()),
                                                       new OleDbParameter("@T_DateTime",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))};
            return (ClassFile.AccessHelper.ExecuteNonQuery(ClassFile.AccessHelper.ConnectionString, " INSERT INTO T_SysLog(T_LogClass,T_LogNote, T_LogIP,T_DateTime) VALUES(@T_LogClass,@T_LogNote, @T_LogIP,@T_DateTime)", parm) > 0) ? true : false;
            }
            catch (Exception EX)
            {
                throw EX;
            }
        }
        /// <summary>
        /// 分页查询日志信息
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        public static DataTable GetAllLogInfo(int PageIndex, int PageSize,out int PageCount,out int RecordCount)
        {
            try
            {
                string strSql = " SELECT * FROM T_SysLog ";
                string strShow = " * ";
                return ClassFile.AccessHelper.ExecutePager(PageIndex, PageSize, "T_LogID", strShow, strSql, "", " T_LogID DESC ", out PageCount, out RecordCount);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 删除指定的日志
        /// </summary>
        /// <param name="strAllLID"></param>
        /// <returns></returns>
        public static bool DelLogInfo(string strAllLID)
        {
            try
            {
                return ClassFile.AccessHelper.ExecuteNonQuery(ClassFile.AccessHelper.ConnectionString, " DELETE FROM T_SysLog WHERE T_LogID IN(" + strAllLID + ")", null) > 0 ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// 删除指定的日志(定期清理日志)
        /// </summary>
        /// <param name="strAllLID"></param>
        /// <returns></returns>
        public static void DelLogInfo()
        {
            try
            {
                if (GetLogCount() > 1000)
                {
                    ClassFile.AccessHelper.ExecuteNonQuery(ClassFile.AccessHelper.ConnectionString, " DELETE FROM T_SysLog WHERE T_LogID NOT IN(SELECT TOP 1000 T_LogID FROM T_SysLog ORDER BY T_LogID DESC )", null);
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取日志数量
        /// </summary>
        /// <param name="strAllLID"></param>
        /// <returns></returns>
        public static int GetLogCount()
        {
            try
            {
                return int.Parse(ClassFile.AccessHelper.ExecuteScalar(ClassFile.AccessHelper.ConnectionString, " SELECT COUNT(*) FROM T_SysLog ", null).ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
